package com.lpit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.constant.Constant;
import com.lpit.dto.AdvertisingDto;
import com.lpit.pojo.Advertising;
import com.lpit.pojo.User;
import com.lpit.service.AdvertisingService;
import com.lpit.service.UserService;
import com.lpit.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 石智星
 * @date 2023/06/13
 * 广告审批
 */
@RestController
@RequestMapping("/advertising")
@Slf4j
public class AdvertisingApprovalController {
    @Autowired
    private AdvertisingService advertisingService;
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{page}/{pageSize}")
    public Result<Page> page(@PathVariable Integer page, @PathVariable Integer pageSize,String name,String state){

        log.info("广告当前页{}",page);
        log.info("每页的数量{}",pageSize);
        Page<Advertising> advertisingPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Advertising> advertisingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        advertisingLambdaQueryWrapper.like(name != null && !"".equals(name),Advertising::getAdvertisingTitle,name);
        advertisingLambdaQueryWrapper.eq(state!=null && !"".equals(state),Advertising::getStatus,state);
        advertisingService.page(advertisingPage,advertisingLambdaQueryWrapper);

        Page<AdvertisingDto> advertisingDaoPage = new Page<>(page,pageSize);
        BeanUtils.copyProperties(advertisingPage,advertisingDaoPage);

        List<Advertising> advertisingList = advertisingPage.getRecords();
        List<AdvertisingDto> advertisingDaoList = new ArrayList<>();
        advertisingList.forEach(advertising -> {
            AdvertisingDto advertisingDao = new AdvertisingDto();
            BeanUtils.copyProperties(advertising,advertisingDao);

            Integer userId = advertising.getUserId();
            User user = userService.getById(userId);
            advertisingDao.setAuthor(user.getName());

            advertisingDaoList.add(advertisingDao);
        });
        advertisingDaoPage.setRecords(advertisingDaoList);

        return Result.success(advertisingDaoPage);
    }
    /**
     * 根据ID查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Advertising> query(@PathVariable Long id){

        Advertising advertising = advertisingService.getById(id);
        log.info("单个广告详细信息{}",advertising);

        return Result.success(advertising);
    }

    /**
     * 广告通过
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result<String> pass(@PathVariable Long id){
        log.info("通过的广告ID{}",id);
        Advertising advertising = advertisingService.getById(id);
        advertising.setStatus(Constant.AUDITED);

        boolean update = advertisingService.updateById(advertising);
        return update ? Result.success(Constant.BY_SUCCESS) : Result.error(Constant.PASS_FAILURE);
    }

    /**
     * 广告驳回
     * @param id
     * @return
     */
    @PutMapping("/reject/{id}")
    public Result<String> reject(@PathVariable Long id){
        log.info("广告驳回的ID{}",id);
        Advertising advertising = advertisingService.getById(id);
        advertising.setStatus(Constant.REJECTED);

        boolean update = advertisingService.updateById(advertising);
        return update ? Result.success(Constant.SUCCESSFUL_REJECTION) : Result.error(Constant.REJECTION_FAILURE);
    }

}
